package com.axeiya.cheddargetter;

import java.util.Date;
import java.util.HashMap;

import org.jdom.Element;

public class Charge {
	

	/**
	 * Your code for this charge. Limited to 36 characters.
	 * REQUIRED
	 */
	private String code;
	
	/**
	 * Positive integer quantity
	 * REQUIRED
	 */
	private int quantity;
	
	/**
	 * Positive or negative integer or float with two digit decimal precision. A positive number will create a charge (debit). A negative number will create a credit.
	 * REQUIRED
	 */
	private double eachAmount;
	
	/**
	 * Description for this charge/credit
	 */
	private String description;
	
	private Date createdDatetime;

	public Charge(String code, int quantity, double eachAmount){
		this.code = code;
		this.quantity = quantity;
		this.eachAmount = eachAmount;
	}
	
	
	
	public Charge(String code, int quantity, double eachAmount, String description,
			Date createdDatetime) {
		super();
		this.code = code;
		this.quantity = quantity;
		this.eachAmount = eachAmount;
		this.description = description;
		this.createdDatetime = createdDatetime;
	}



	public static Charge parseCharge(Element charge){
		String code = charge.getChildText("code");
		int quantity = Integer.valueOf(charge.getChildText("quantity"));
		float eachAmount = Float.valueOf(charge.getChildText("eachAmount"));
		String description = charge.getChildText("description");
		Date createdDatetime = CGTools.parseDateTime(charge.getChildText("createdDatetime"));
		
		
		return new Charge(code, quantity, eachAmount, description, createdDatetime);
	}
	
	public static HashMap<String, String> formatCharge(Charge charge){
		HashMap<String, String> parameters = new HashMap<String, String>();
		
		if(charge.getCode() != null && !charge.getCode().isEmpty())
			parameters.put("chargeCode", charge.getCode());
		else{
			//TODO: throw Required exception
		}
		
		if(charge.getQuantity() >= 0){
			parameters.put("quantity", String.valueOf(charge.getQuantity()));
		}
		else{
			//TODO: throw quantity positive exception
		}
		
		parameters.put("eachAmount", String.valueOf(charge.getEachAmount()));
		
		if(charge.getDescription() != null && !charge.getDescription().isEmpty())
			parameters.put("description", charge.getDescription());
		
		return parameters;
		
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public double getEachAmount() {
		return eachAmount;
	}

	public void setEachAmount(double eachAmount) {
		this.eachAmount = eachAmount;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	
	public Date getCreatedDatetime() {
		return createdDatetime;
	}

	public void setCreatedDatetime(Date createdDatetime) {
		this.createdDatetime = createdDatetime;
	}
}
